<template>
    <div class="comment-container">
        <h3 class="comment-title">评论内容</h3>
        <hr>
        <textarea placeholder="请输入评论内容（最多不超过100个字" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postcomment">发表评论</mt-button>
        <div class="comment-list">
            <div class="comment-item" v-for="(item,i) in commentlist" :key="item.add_timed">
                <div class="comment-title">
                    第{{i+1}}楼 &nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time|dateFormat}}
                </div>
                <div class="comment-body">
                    {{item.content === 'undefined' ?'此用户很懒，什么也没说':item.content}}
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'

    export default {
        data() {
            return {
                commentlist: [],
                pageindex: 1,
                msg:''
            }
        },
        props: ['id'],
        created() {
            this.getcomment()
        },
        methods: {
            getcomment() {
                this.$http.get("api/getcomments/" + this.id + "?pageindex=" + this.pageindex)
                    .then(result => {
                        if (result.body.status === 0) {
                            // this.commentlist = result.body.message;
                            this.commentlist = this.commentlist.concat(result.body.message)
                        } else {
                            Toast("获取评论失败")
                        }
                    })
            },
            getmore(){
               this.pageindex++;
               this.getcomment();
            },
            postcomment(){
                if(this.msg.trim().length === 0){
                    return Toast('评论不能为空')
                }
                this.$http.post("api/postcomment/"+this.$route.params.id,{content:this.msg.trim()})
                    .then(result => {
                        if(result.body.status === 0){
                            var comment = {
                                user_name:"匿名用户",
                                add_time: new Date(),
                                content:this.msg.trim()
                            };
                            this.commentlist.unshift(comment);
                            this.msg = ''
                        }else {
                            Toast('评论发表失败')
                        }
                    })
            }

        }
    }
</script>

<style lang="scss" scoped>
    .comment-container {
        background-color: #fff;
        h3 {
            font-size: 18px;
        }
        comment-title {
        }
        textarea {
            margin: 0;
            font-size: 14px;
        }
        .comment-list {
            margin-top: 5px;
            .comment-item {
                .comment-title {
                    font-size: 13px;
                    background-color: #ccc;
                    line-height: 30px;
                }
                .comment-body {
                    font-size: 14px;
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }
    }
</style>